﻿using Sunny.UI;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SmartMotion.Core;
using SmartMotion.Core.Mgrs;
using SmartMotion.Core.Reflection;

namespace SmartMotion.AllPages.SysConfig
{
    public partial class FrmStationConfig : UIPage
    {
        private List<DllTypeInfo> _stationsCanUse;//可用工站类型
        private ConcurrentDictionary<string, VarInfo> _stations;//已添加工站
        public FrmStationConfig()
        {       
            InitializeComponent();
        }

        private void FrmStationConfig_Load(object sender, EventArgs e)
        {
            _stationsCanUse = UnityIoc.Ins.LoadDllFromDirectory<StationBase>();
            BindingSource bs = new BindingSource();
            bs.DataSource = _stationsCanUse;
            cmbStations.DataSource = bs;
            cmbStations.DisplayMember = "TypeName";

            _stations = StationsMgr.Ins.Stations;
            ShowStation();
        }

        void ShowStation()
        {
            lstStation.Items.Clear();
            foreach (var station in _stations)
            {
                lstStation.Items.Add(station.Key);
            }
        }
            
        private void btnAdd_Click(object sender, EventArgs e)
        {

            DllTypeInfo dllTypeInfo= cmbStations.SelectedItem as DllTypeInfo;
            var type = Assembly.LoadFile(dllTypeInfo.Dll).GetType(dllTypeInfo.TypeName);
            if (type != null)
            {
                if (string.IsNullOrEmpty(txtStationName.Text))
                {
                    ShowErrorDialog("请输入工站名称");
                    return;
                }
                StationBase sb = (StationBase)Activator.CreateInstance(type);
                if (_stations.ContainsKey(txtStationName.Text))
                {
                    ShowErrorDialog("输入了一个已存在的工站名称");
                }
                else
                {
                    sb.Name = txtStationName.Text;
                    _stations.TryAdd(txtStationName.Text, new VarInfo() { Group = "station",Type =dllTypeInfo.TypeName ,Name = txtStationName.Text, Value = sb });
                    lstStation.Items.Add(txtStationName.Text);
                }
               
            }   
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            StationsMgr.Ins.Save();
        }

        private void lstStation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstStation.SelectedItem != null)
            {
                string sbName = lstStation.SelectedItem.ToString();
                if (_stations.TryGetValue(sbName,out var varInfo))
                {
                    StationBase sb = (StationBase) varInfo.Value;
                    var page= sb.GetConfigPage();
                    panStation.Controls.Clear();
                    panStation.Controls.Add(page);
                    page.Show();
                }
            }
        }
    }
}   
